package com.prs.crm.action.sale;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import com.prs.crm.domain.sale.ReturnsType;
import com.prs.crm.service.sale.ReturnsTypeService;

public class ReturnsTypeAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private ReturnsType type;

	private Integer[] typeIds;

	private ReturnsTypeService returnsTypeService;

	private PaginationSupport pagination;

	@Action("listReturnsType")
	public String listReturnsType() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				ReturnsType.class);
		this.setPagination(this.getReturnsTypeService().listByPage(criteria,
				getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}

	@Action("editReturnsType")
	public String editReturnsType() {
		return SUCCESS;
	}

	@Action(value = "saveReturnsType", results = { @Result(name = "success", type = "redirect", location = "listReturnsType"),
			@Result(name="error",type="chain",location="editReturnsType")})
	public String saveReturnsType() {
		if(type.getName() == null || type.getName().equals("")) {
			this.addActionError("退货类型名称不能为空");
			return ERROR;
		}
		for(ReturnsType returnsType:this.getReturnsTypeService().getAllType()){
			if(returnsType.getName().trim().equals(type.getName().trim())){
				this.addActionError("退货类型名称已经存在");
				return ERROR;
			}
		}
		this.getReturnsTypeService().save(type);
		return SUCCESS;
	}

	@Action(value = "removeReturnsType", results = { @Result(name = "success", type = "redirect", location = "listReturnsType") })
	public String removeReturnsType() {
		this.getReturnsTypeService().batchRemove(typeIds);
		return SUCCESS;
	}

	public ReturnsType getType() {
		return type;
	}

	public void setType(ReturnsType type) {
		this.type = type;
	}

	public Integer[] getTypeIds() {
		return typeIds;
	}

	public void setTypeIds(Integer[] typeIds) {
		this.typeIds = typeIds;
	}

	public ReturnsTypeService getReturnsTypeService() {
		return returnsTypeService;
	}

	public void setReturnsTypeService(ReturnsTypeService returnsTypeService) {
		this.returnsTypeService = returnsTypeService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

}
